/*
	       File: SendMail.cs
	Create Date: 2007-11-29
	    Version:

	Description:
Send an email.


*/
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Net.Mail;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using GuerillaProgrammer.Components;
using GuerillaProgrammer.Components.Logging;

namespace GuerillaProgrammer.FileProcessor.Activities
{
    public partial class SendMail : Activity
    {
        #region Private member fields
        private string _MailServer;
        private string _EmailAddress;
        private string _FromAddress;
        private string _Subject;
        private string _Body;
        private bool _PullBodyFromFile;
        private string _Attachment;
        #endregion

        #region Constructors and cleanup methods
        public SendMail()
        {
            InitializeComponent();
        }
        #endregion

        #region Private implementation methods
        private string GetBody()
        {
            if (true == _PullBodyFromFile)
            {
                if (true == File.Exists(_Body))
                {
                    StreamReader reader = new StreamReader(_Body);
                    string body = reader.ReadToEnd();
                    reader.Close();
                    return body;
                }
                else
                {
                    Logger.LogError("File does not exist!", "The specified file does not exist.\nFile: {0}", _Body);
                    return string.Empty;
                }
            }
            else
            {
                return _Body;
            }
        }
        #endregion

        #region Class properties
        public static DependencyProperty MailServerProperty = System.Workflow.ComponentModel.DependencyProperty.Register("MailServer", typeof(string), typeof(SendMail));
        [Description("Address of the mail server")]
        [Category("Send Email")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string MailServer
        {
            get { return _MailServer; }
            set { _MailServer = value; }
        }
        public static DependencyProperty EmailAddressProperty = System.Workflow.ComponentModel.DependencyProperty.Register("EmailAddress", typeof(string), typeof(SendMail));
        [Description("Email Address")]
        [Category("Send Email")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string EmailAddress
        {
            get { return _EmailAddress; }
            set { _EmailAddress = value; }
        }
        public static DependencyProperty FromAddressProperty = System.Workflow.ComponentModel.DependencyProperty.Register("FromAddress", typeof(string), typeof(SendMail));
        [Description("From Email Address")]
        [Category("Send Email")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string FromAddress
        {
            get { return _FromAddress; }
            set { _FromAddress = value; }
        }
        public static DependencyProperty SubjectProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Subject", typeof(string), typeof(SendMail));
        [Description("Subject")]
        [Category("Send Email")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string Subject
        {
            get { return _Subject; }
            set { _Subject = value; }
        }
        public static DependencyProperty BodyProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Body", typeof(string), typeof(SendMail));
        [Description("Body")]
        [Category("Send Email")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string Body
        {
            get { return _Body; }
            set { _Body = value; }
        }
        public static DependencyProperty PullBodyFromFileProperty = System.Workflow.ComponentModel.DependencyProperty.Register("PullBodyFromFile", typeof(bool), typeof(SendMail));
        [Description("Body is file a filepath.")]
        [Category("Send Email")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool PullBodyFromFile
        {
            get { return _PullBodyFromFile; }
            set { _PullBodyFromFile = value; }
        }
        public static DependencyProperty AttachmentProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Attachment", typeof(string), typeof(SendMail));
        [Description("Attachment")]
        [Category("Send Email")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string Attachment
        {
            get { return _Attachment; }
            set { _Attachment = value; }
        }
        #endregion

        #region Events handlers
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            Logger.LogEnter();

            IProcessorDataService dataService = executionContext.GetService(typeof(IProcessorDataService)) as IProcessorDataService;
            if (null == dataService)
            {
                throw new InvalidOperationException("Can't get the service!");
            }
            WorkItem workItem = dataService.GetWorkItem();
            StringProcessor stringProcessor = new StringProcessor(new FileProcessorSymbolTable(dataService.GetConfiguration(), workItem));
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(_FromAddress);
            mail.To.Add(new MailAddress(_EmailAddress));
            mail.Subject = _Subject;

            if (true == _PullBodyFromFile)
            {
                string filePath = stringProcessor.SubstituteStringValues(_Body);
                if (true == File.Exists(filePath))
                {
                    StreamReader reader = new StreamReader(filePath);
                    mail.Body = reader.ReadToEnd();
                    reader.Close();
                    
                }
                else
                {
                    Logger.LogError("File does not exist!", "The specified file does not exist.\nFile: {0}", filePath);
                }
            }
            else
            {
                mail.Body = _Body;
            }

            if (string.Empty != _Attachment)
            {
                string processedPath = stringProcessor.SubstituteStringValues(_Attachment);
                Attachment theAttachment = new Attachment(processedPath);
                mail.Attachments.Add(theAttachment);
            }

            Logger.LogInfo("Send Email", "email sent. address: {0}", _EmailAddress);
            SmtpClient client = new SmtpClient(_MailServer);
            client.Send(mail);

            Logger.LogLeave();
            return ActivityExecutionStatus.Closed;
        }
        #endregion
	}
}
